﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication13
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] ar = { "d", "zz", "vb", "abc", "ded", "ab", "abc", "pl","a", "z" };
            int alg = 0;
            for (int i = 1; i < ar.Length; i++)
            {
                for (int j = ar.Length - i - 1; j < ar.Length - 1; j++)
                {
                    int k = 0;
                    while (k < Math.Min(ar[j].Length, ar[j + 1].Length))
                    {
                        alg++;
                        int comp = CompareString(ar[j], ar[j + 1]);
                        char s1 = ar[j][k];
                        char s2 = ar[j + 1][k];
                        if (s1 > s2)
                        {
                            string tmp = ar[j];
                            ar[j] = ar[j + 1];
                            ar[j + 1] = tmp;
                        }
                        else if (s1 == s2)
                        {
                            k++;
                            if (comp == 1 && k == ar[j + 1].Length - 1)
                            {
                                string tmp = ar[j];
                                ar[j] = ar[j + 1];
                                ar[j + 1] = tmp;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            Console.WriteLine("complexity: " + alg);
            foreach (var el in ar)
            {
                Console.WriteLine(el);
            }

        }
        static int CompareString(string s1, string s2)
        {
            int resComp = -1;
            if (s1.Length < s2.Length)
            {
                resComp = -1;
            }
            else if (s1.Length > s2.Length)
            {
                resComp = 1;
            }
            else
            {
                resComp = 0;
            }
            return resComp;
        }


    }
}
